package cn.yunyichina.provider.register.utils;

import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.CacheService;
import cn.yunyichina.provider.iface.entity.base.Rule;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.register.constant.CacheConstant;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 挂号工具类
 * Created by Administrator on 2017/3/10.
 */
public class RegUtil {
    private static final Logger logger = LoggerFactory.getLogger(RegUtil.class);

    public final static String REG_TYPE_DUTY ="1";//当班挂号
    public final static String REG_TYPE_APPOINTMENT ="2";//预约挂号

    public final static String REG_TYPE ="regType";//该医院支持的挂号模式 1，2
    public final static String ON_DUTY_REG_TIME ="onDutyRegTime";// 当班挂号允许挂号时间段
    public final static String APPOINTMENT_TOMORROW_ENDTIME ="appointmentTomorrowEndTime";// 允许预约明天号的截止时间点
    public final static String ONLINE_PAYMENT_CONTROL ="onlinePaymentControl";// 支付控制
    public final static String ONLINE_PAYMENT_CONTROL_APPOINTMENT ="onlinePaymentControlAppointment";// 支付控制
    public final static String IS_SUPPORT_REFUND_ON_DUTY ="isSupportRefundOnDuty";// 当天挂号是否支持取消
    public final static String IS_SUPPORT_REFUND_APPOINTMENT ="isSupportRefundAppointment";// 预约挂号是否支持取消
    public final static String CANCEL_ON_DUTY_END_TIME ="cancelOnDutyEndTime";// 取消当班挂号截止时间点
    public final static String CANCEL_APPOINTMENT_END_TIME ="cancelAppointmentEndTime";// 取消预约挂号截止时间点
    public final static String CALENDAR_DAYS_TYPE ="calendarDaysType";// 预约挂号日历显示天数
    public final static String PAYMENT_TIMEOUT_TIME ="paymentTimeOutTime";// 支付超时时间，单位：分钟
    public final static String STOP_REG_TIME_LIMIT ="stopRegTimeLimit";// 停诊退费的时间限制
    public final static String IS_STOP_REG_REFUND_HIS ="isStopRegRefundHis";// 停诊是否执行退费通知
    public final static String IS_STOP_REG_UNLOCK ="isStopRegUnlock";// 停诊是否执行号源释放
    public final static String acceptStopRegType ="acceptStopRegType";// 停诊信息方式
    public final static String STOP_REG_MSG_TYPE ="stopRegMsgType";//停诊消息推送方式
    public final static String RESERVE_NEED_LOCK ="reserveNeedLock";//预约取号是否需要锁号
    public final static String RESERVATION_RULE ="reservationRule";//预约取号是否需要锁号
    public final static String DOCTOR_SOURCE_CACHE_MINUTE ="doctorSourceCacheMinute";//号源缓存过期时间
    public final static String IS_QUERY_DOCTOR ="isQueryDoctor";//号源缓存过期时间


    public final static String NO_ONDUTY_REG_MSG ="该医院不支持当班挂号";
    public final static String NO_ON_APPOINTMENT_REG_MSG ="该医院不支持预约挂号";
    public static final String NOT_ON_DUTY_REG_TIMESTART_MSG = "还未到当班挂号的开始时间.请稍候.";
    public static final String NOT_ON_DUTY_REG_TIMEEND_MSG = "已过当班挂号的截至时间.请选择其它日期.";
    public static final String NOT_APPOINTMENT_TOMORROW_ENDTIME_MSG = "已过预约明天号的截止时间.请选择其它日期.";
    public static final String NOT_SUPPORT_REFUND_ON_DUTY_MSG = "该医院不支持当班挂号取消";
    public static final String NOT_SUPPORT_REFUND_APPOINTMENT_MSG = "该医院不支持预约挂号取消";
    public static final String OVER_REFUND_ENDTIME_MSG = "超过取消挂号截止时间";


    /**
     * 根据挂号日期，判断是当班挂号还是预约挂号
     * @param scheduleTime
     * @return 1：当天挂号 2：预约挂号
     */
    public static String getRegType(String scheduleTime){
        String regType;//1：当天挂号 2：预约挂号
        try {
            Date scheduleDate = DateUtils.StringToDateYMD(scheduleTime);
            Date today = DateUtils.StringToDateYMD(DateUtils.today());

            if (scheduleDate.compareTo(today) > 0) {
                regType = REG_TYPE_APPOINTMENT;
            } else {
                regType = REG_TYPE_DUTY;
            }
        }catch (Exception e){
            regType = REG_TYPE_DUTY;
            e.printStackTrace();
        }
        return regType;
    }

    public static String getRule(String hospitalCode,String attributeName,String defaultValue){
//        String cacheValue = CacheService.getGlobalKey(CacheConstant.CACHE_REGISTER_MODULE,CacheConstant.CACHE_REGISTER_RULE,hospitalCode);
//        if(StringUtils.isBlank(cacheValue)) {
//            logger.info("请先设置规则到缓存中:" + hospitalCode);
//            return defaultValue;
//        }
//
//
//        List<Rule> ruleList = JsonUtils.parseArray(cacheValue,Rule.class);
//        for(Rule rule : ruleList){
//            if(attributeName.equals(rule.getAttributeName())){
//                value = rule.getAttributeProperties();//单位：分钟
//                break;
//            }
//        }
        String value = null;
        Map<String,String> ruleMap = new HashMap<String,String>();
        ruleMap.put("attribute_name" , attributeName);
        ruleMap.put("org_code" , hospitalCode);
        Response ruleResp = BaseService.callHumpFromInternal("yyt.base.rule.select", JSON.toJSONString(ruleMap));
        logger.info("RegUtil yyt.base.rule.select response=" + JsonUtils.toJsonString(ruleResp));
        if("0".equals(ruleResp.getResultCode())){
            Rule rule=JsonUtils.parseObject(ruleResp.getResult(), Rule.class);
            if(rule != null && attributeName.equals(rule.getAttributeName())){
                value = rule.getAttributeProperties();
            }
        }

        if(StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value;
    }


}
